package LoginUp;
/**
 * tijiao.jsp
 * 登录逻辑控制，将得到的用户名与密码与数据库中已存在的用户名和密码进行比较。
 * 如果相同则进行跳转到登陆成功页面 ，否则跳回登录页面。
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginUp extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端以UTF-8编码进行输出
        resp.setCharacterEncoding("UTF-8");
        //设置浏览器以UTF-8编码进行接收,解决中文乱码问题
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String username = req.getParameter("username");
        String passwd = req.getParameter("passwd");
        RequestDispatcher dis = null;
        System.out.println(username + passwd);
        try {
            try {
                String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Connection comm = DriverManager.getConnection(url);
                String SQL = "SELECT * FROM [user] where(username=? and password =?)";//注意：表名一定要带[]
                //结果集
                PreparedStatement pstmt = comm.prepareStatement(SQL);
                pstmt.setString(1, username);
                pstmt.setString(2, passwd);
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    if (rs != null) {
                        rs.close();
                    }
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (comm != null) {
                        comm.close();
                    }
//                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
//                    dis.forward(req,resp);
                    resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/hello.jsp");
                    System.out.println("hah");
                } else {
                    if (rs != null) {
                        rs.close();
                    }
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    if (comm != null) {
                        comm.close();
                    }
//                    dis = req.getRequestDispatcher("http://localhost:8080/javaWeb_war_exploded/fruit.jsp");
//                    dis.forward(req,resp);
                    resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/Firstpage.jsp");
                    System.out.println("ggggggggg");
                }

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
